import random

from init_db import conn
import time
from random_id import IdWorker
from datetime import datetime, timedelta

visible = [0, 1]
default = [0, 1]
channel_datas = []
worker = IdWorker(1, 2, 0)

def init_channel(datas):
    for data in datas:
        channel_id = worker.get_id()
        channel_name = data
        update_time = datetime.now()
        create_time = datetime.now()
        sequence = random.randint(1, 1000)
        is_visible = random.choice(visible)
        is_default = random.choice(default)
        channel_data = (channel_id, channel_name, create_time, update_time, sequence, is_visible, is_default)
        channel_datas.append(channel_data)
    _values = "%s,%s,%s,%s,%s,%s,%s"
    sql = """insert into news_channel(channel_id, channel_name, create_time, update_time, sequence, is_visible, is_default) values(%s) """ % _values
    print(sql)
    cursor = conn.cursor()
    cursor.executemany(sql, channel_datas)
    conn.commit()
    cursor.close()
    print("初始化news_channel表成功！")


# 加载文本数据

# if __name__ == '__main__':
#     with open('../dataset/channel.txt', 'r', encoding='UTF-8') as file:
#         datas = file.read().splitlines()
#     init_channel(datas)
#     print(datas)
